package hms.inventory;

import hms.database.DAO;
import hms.master.MasterController;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.DefaultListModel;

/**
 * ItemCategories is a collection of item categories
 * 
 * 
 * @author Akilah McIntyre
 * 
 */
public class ItemCategories extends DefaultListModel {
	public ItemCategory invItemCategory;

	/**
	 * // TODO [Akilah] Add Comments
	 */
	public ItemCategories() {
	}

	/**
	 * Initialize itemCategories from database
	 */
	public void initItemCategories() {
		int itemCategory_id = -1; // unique identifier
		String itemCategory_name = ""; // name
		String size_count = "select count(*) from ITEM_CATEGORY where ITEM_CATEGORY_ID =99 and ITEM_CATEGORY_NAME='UNDEFINED'";
        int undfCount = 0;
        
		try {
			ResultSet rsCount = MasterController.getDatabaseObject().getResult(size_count);
        	while(rsCount.next()){
        		undfCount = rsCount.getInt(1);
        		if(undfCount == 0){
        			MasterController.getDatabaseObject().updateCommand("INSERT INTO ITEM_CATEGORY(ITEM_CATEGORY_ID,ITEM_CATEGORY_NAME) VALUES(99,'UNDEFINED')");
        		}
        	}
    		
			// get data from database associated with dao
			ResultSet rsItemCategories = MasterController
					.getDatabaseObject()
					.getResult(
							"SELECT ITEM_CATEGORY_ID, ITEM_CATEGORY_NAME FROM ITEM_CATEGORY WHERE ITEM_CATEGORY_ID != 99 ORDER BY ITEM_CATEGORY_ID");
			// Create ItemCategory instance from database
			while (rsItemCategories.next()) {
				itemCategory_id = rsItemCategories.getInt("ITEM_CATEGORY_ID");
				itemCategory_name = rsItemCategories
						.getString("ITEM_CATEGORY_NAME");
				ItemCategory itemCategory_item = new ItemCategory(
						itemCategory_id, itemCategory_name);
				this.addElement(itemCategory_item); // add to itemCategories
													// model
			}
		} catch (SQLException e) {
			// TODO [Akilah] Write to view or popup dialog
			e.printStackTrace();
		}
	}

	/**
	 * Adds item category
	 * 
	 * @param next_itemCategory_id
	 *            unique identifier
	 * @param new_itemCategory_name
	 *            name of new itemCategory
	 */
	public void addItemCategory(int next_itemCategory_id,
			String new_itemCategory_name) {
		this.addElement(new ItemCategory(next_itemCategory_id,
				new_itemCategory_name));
	}

	/**
	 * Adds item category
	 * 
	 * @param new_itemCategory_name
	 *            name of new itemCategory
	 */
	public boolean addDbItemCategory(String new_itemCategory_name) {
		boolean isSuccess = false;
		// Get new id as new id may not be +1
		int next_itemCategory_id = MasterController.getDatabaseObject()
				.addDBTableValueParam("ITEM_CATEGORY", "ITEM_CATEGORY_NAME",
						"ITEM_CATEGORY_ID", new_itemCategory_name);
		if (next_itemCategory_id > -1) { // if the insertion was successful
			this.addElement(new ItemCategory(next_itemCategory_id,
					new_itemCategory_name));
			isSuccess = true;
		}

		return isSuccess;
	}

	/**
	 * @param item_category_name
	 *            New Name of ItemCategory
	 * @param selIndex
	 *            Index of target item_category
	 * @return if database update is success
	 */
	public boolean updateDbItemCategory(String item_category_name, int selIndex) {
		boolean isSuccess = false;
		int item_category_id = -1;
		try {
			item_category_id = ((ItemCategory) this.get(selIndex)).itemCategory_id;
			isSuccess = MasterController.getDatabaseObject()
					.updateDBTableValueParam("ITEM_CATEGORY",
							"ITEM_CATEGORY_NAME", "ITEM_CATEGORY_ID",
							item_category_name, item_category_id);
			if (isSuccess) {
				this.changeItemCategoryName(item_category_name, selIndex);
			}
		} catch (Exception ex) {
			isSuccess = false;
		}
		return isSuccess;
	}

	/**
	 * @param item_category_name
	 *            New Name of ItemCategory
	 * @param selIndex
	 *            Index of target item_category
	 */
	public void changeItemCategoryName(String item_category_name, int selIndex) {
		ItemCategory destItemCategory = ((ItemCategory) this.get(selIndex));
		destItemCategory.itemCategory_name = item_category_name;
	}
	
	public boolean deleteDbItemCategory(String item_category_name, int selIndex) {
		boolean isSuccess = false;
		int item_category_id = -1;
		try {
			item_category_id = ((ItemCategory) this.get(selIndex)).itemCategory_id;
			MasterController.getDatabaseObject().updateCommand("UPDATE INVENTORY SET ITEM_CATEGORY_ID = 99 WHERE ITEM_CATEGORY_ID = " + item_category_id);
			isSuccess = MasterController.getDatabaseObject()
					.deleteDBTableValueParam("ITEM_CATEGORY", "ITEM_CATEGORY_NAME", "ITEM_CATEGORY_ID",
							item_category_name, item_category_id);
			if (isSuccess) {
				this.deleteItemCategoryName(selIndex);
			}
		} catch (Exception ex) {
			isSuccess = false;
		}
		return isSuccess;
	}

	/**
	 * @param selIndex
	 *            Index of target item_category
	 */
	public void deleteItemCategoryName(int selIndex) {
		this.remove(selIndex);
		
	}
}

